package org.yufan.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.yufan.bean.User;
import org.yufan.exception.CustomException;
import org.yufan.service.UserFrontService;
import org.yufan.utils.HttpClientUtil;
import org.yufan.utils.JsonUtils;

/**
 * Created by zxd on 2018/4/12.
 */
@Service
public class UserFrontServiceImpl implements UserFrontService{

    public static final String QUERY_USER_URL="http://sso.yufan.com/user/";

    @Override
    public User queryUserByToken(String token) throws CustomException {

        try {
            String json = HttpClientUtil.doGet(QUERY_USER_URL + token);
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(json);
            String user_json=jsonNode.get("data").toString();
            return JsonUtils.jsonToPojo(user_json,User.class);
        }catch (Exception e){
            e.printStackTrace();;
            throw  new CustomException("用户信息读取失败!");
        }
    }
}
